<style type="text/css">
    .table th a, .table td a.submitdelete {
        color: #FF0000 !important; display: inline;
    }
    .table th a:hover, .table td a.submitdelete:hover {
        color: #FFFFFF !important;
        text-decoration: none !important;
    }
</style>
<div class="container">

    <div class="page-header">
        <div class="row">
            <h1 class="span8">Gallery :: <?=ucfirst($gallery['title'])?></h1>
            <span class="span4 clearfix">
                <a class="nav-control new-page w-18" href="<?=base_url($link.'/view_new?post_type=gallery')?>">
                    Add Folder
                </a>
            </span>
        </div>


    </div>

    <div class="row">

        <form accept-charset="utf-8" action="<?=base_url()?><?=$link?>/submit_gallery" method="POST" id="updategallery_form">

            <input type="hidden" value="edit-gallery" name="post_action" id="post_action">
            <input type="hidden" value="<?=$gallery['ID']?>" name="gallery_ID">

            <div class="span12 wrap ui-tabs ui-widget ui-widget-content" style="border: 0; padding: 0;">

                <div class="ui-tabs-panel ui-widget-content ui-corner-all" style="border-width: 1px;">

                    <h2>Document settings</h2>

                    <table class="form-table"> 
                        <tbody>


                            <tr valign="top"> 

                                <td class="upload_text">Title:</td> 

                                <td>

                                    <input type="text" value="<?=$gallery['title']?>" name="galleryname" size="43">

                                    <i>( Allowed characters are: a-z, A-Z, 0-9, _ )</i>

                                </td>

                            </tr>

                            <tr valign="top"> 

                                <td class="upload_text">Link to:</td> 

                                <td>           

                                    <select id="linkto" name="linkto">

                                        <option value="0" selected="selected">--Please select </option>

                                        <? foreach(config_item('page_types') as $page_type): ?>

                                            <optgroup label="<?=ucfirst($page_type);?>">

                                            <? $pages = $this->pages_model->view_all($page_type); $selected = ''; ?>                                                 

                                            <? foreach($pages as $page):?>

                                                <? if($page['ID']==$gallery['posts_ID']) $selected = 'selected="selected"'; ?>

                                                <option <?=$selected?> value="<?=$page['ID']?>"> <?=$page['name']?></option>

                                                <? $children = $this->pages_model->view_all_children($page['ID']); $selected = ''; ?>

                                                <? if(count($children)>0):
                                                        foreach($children as $child):?>

                                                        <? if($child['ID']==$gallery['posts_ID']) $selected = 'selected="selected"'; ?>

                                                        <option <?=$selected?> value="<?=$child['ID']?>">-- <?=$child['name']?></option>

                                                        <? $selected = ''; endforeach; ?>

                                                    <? endif; ?>

                                                <? endforeach; ?>

                                            <? endforeach; ?>
                                    </select>

                                </td> 

                            </tr>

                            <tr valign="top"> 

                                <td class="upload_text">Parent:</td> 

                                <td>           

                                    <select id="parent_id" name="parent_id">
                                        <option value="0" selected="selected">(no parent)</option>

                                        <? foreach(config_item('page_types') as $page_type): 

                                                $galleries = $this->document_model->view_all($page_type);

                                                /*$this->firephp->log($this->db->last_query());
                                                $this->firephp->log($galleries);*/

                                                if($page_type=='news')$page_type= 'Latest News';?>

                                            <optgroup label="<?=ucfirst($page_type);?>">

                                            <? foreach($galleries as $pg): $selected = ''; ?>

                                                <? if($gallery['ID']!=$pg['ID']): ?>

                                                    <? if($pg['ID']==$gallery['parent_ID']) $selected = 'selected="selected"'; ?>

                                                    <option <?=$selected?> value="<?=$pg['ID']?>"><?=$pg['title']?></option>

                                                    <? endif; ?>

                                                <? endforeach; ?>                                                                                                           

                                            <? endforeach; ?>

                                    </select>

                                    <br>                
                                    <br>

                                </td> 

                            </tr> 

                        </tbody>

                    </table>

                    <div class="submit row"> 

                        <span class="span2 ml-15> save2" style="width: 125px !important;"><input type="submit" value="Edit document" class="btn" id="edit_gallery" disabled="disabled"></span>
                        <span class="span4 loader"><img id="ajax-loading" class="ajax-loading" src="<?=base_url()?>assets/img/backgnds/loading.gif" style="visibility: hidden;"></span>

                    </div>

                </div>

            </div>

        </form>

    </div>

    <br><br>

    <? if(count($images)>0):?>

        <form accept-charset="utf-8" action="<?=base_url()?><?=$link?>/files_order" method="POST">
            <input type="hidden" value="<?=$gallery['ID']?>" name="gallery_ID">

            <h2> Pictures </h2>

            <div class="row">

                <div class="span12">

                    <table class="table table-striped table-bordered table-condensed">
                        <thead>
                            <tr>
                                <th>Thumbnail</th>
                                <th>Filename</th>
                                <th class="center">Order</th>
                            </tr>
                        </thead>
                        <tbody>

                            <? $k=1; foreach($images as $picture): ?> 


                                <!-- HELPER  -->
                                <?

                                    $pic_pices = explode('.',$picture['name']);
                                    $pic_title = $pic_pices[0];
                                    $pic_arr = explode('.',$picture['filename']);
                                    $extension = $pic_arr[1];

                                ?>

                                <tr>

                                    <td class="column-thumbnail">
                                        <a title="<?=$picture['filename']?>" href="<?=base_url()?>pro-documents/<?=$gallery['path']?>/<?=$picture['filename']?>"  target="_blank">

                                            <img src="<?=base_url()?>assets/icons/pro-documents/<?=$extension?>.png" />

                                        </a>
                                    </td>
                                    <td>
                                        <div><strong><?=$picture['name']?></strong></div>

                                        <div><?=date('m.d.Y',$picture['date_time'])?></div>
                                        <br>

                                        <div>
                                            <a title="View &quot;<?=$picture['name']?>&quot;" href="<?=base_url()?>pro-documents/<?=$gallery['path']?>/<?=$picture['filename']?>" class="shutter" target="_blank">View</a> | 

                                            <a onclick="javascript:check=confirm( 'Delete &quot;<?=$picture['name']?>&quot; ?');if(check==false) return false;" href="<?=base_url()?><?=$link?>/delete_file?picture_ID=<?=$picture['ID']?>&gallery_ID=<?=$gallery['ID']?>" class="submitdelete">Delete</a>

                                        </div>

                                    </td>
                                    <td class="column-order">

                                        <input name="picture-order[]" value="<?=$picture['order']?>" tabindex="<?=$k+5?>" />

                                    </td>

                                </tr>


                                <? $k++; endforeach; ?>
                        </tbody>
                    </table>

                </div>

            </div>

            <div class="form-actions">

                <div class="row">

                    <span class="span4 save"><button class="btn btn-primary" type="submit"><i class="icon-white icon-download-alt"></i> Save changes</button></span>
                    <span class="span4 loader"><img id="ajax-loading" class="ajax-loading" src="<?=base_url()?>assets/img/backgnds/loading.gif" style="visibility: hidden;"></span>
                </div>

            </div>

        </form>

        <? endif; ?>

</div> <!-- /container -->

<!--EDIT Gallery SCRIPT-->
<script type="text/javascript">

    // Override default error message
    jQuery.validator.messages.required = "";

    jQuery.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            var check = false;
            return this.optional(element) || regexp.test(value);
        },
        "Please check your input."
    );


    // Override generation of error label
    jQuery("#updategallery_form").validate({
            rules: {
                galleryname: {
                    required: true,
                    /*regex: /^\s*[A-Za-z0-9 -_\s]+s*$/*/
                    regex: /^\w+$/
                }
            },
            submitHandler: function(form) {

                if(jQuery('input[name="galleryname"]').val()==""){
                    alert('Enter gallery name');
                    return false;
                }

                jQuery('#edit_gallery').attr('disabled','disabled');
                var form = jQuery("#updategallery_form");
                var form_data = form.serialize();

                jQuery('.ajax-loading').css('visibility','visible');

                jQuery.ajax({
                        url: form.attr('action'),
                        dataType: 'json',
                        type: "POST",
                        data: form_data,
                        success: function (data, textStatus, xhr) {
                            jQuery('.ajax-loading').css('visibility','hidden');

                            if(data.action){
                                jQuery('.loader').html(data.msg);
                                // REDIRECT
                                function promo_show(){
                                    window.location.href = base_url+'<?=$link?>'+'/view_all?post_type=<?=$post_type?>'+'&type=all'; 
                                }
                                window.setTimeout(function() { promo_show(); }, 1001);
                            }else{
                                alert(data.msg);
                                jQuery('#edit_gallery').removeAttr('disabled');                            
                            }

                        },
                        error: function (xhr, textStatus, errorThrown) {
                            jQuery('.ajax-loading').css('visibility','hidden');
                            alert('Error ocured...\n Server not found.');
                            jQuery('#edit_gallery').removeAttr('disabled');

                        }
                });

                return false;

            },
            errorPlacement: function(error, element){}


    });

    jQuery('#edit_gallery').removeAttr('disabled');

</script> 

<script type="text/javascript">
    jQuery(function() {

            jQuery('a[rel=lightbox]').lightBox({   
                    overlayBgColor: '#000',
                    overlayOpacity: 0.6,
                    imageLoading: base_url+'assets/img/lightbox/loading.gif',
                    imageBtnClose: base_url+'assets/img/lightbox/close.gif',
                    imageBtnPrev: base_url+'assets/img/lightbox/prev.gif',
                    imageBtnNext: base_url+'assets/img/lightbox/next.gif',
                    imageBlank : base_url+'assets/img/lightbox/blank.gif',
                    containerResizeSpeed: 350,
                    txtImage: 'Image',
                    txtOf: 'of'
            });
    });
</script>